#
#	I:  INPUT
#	O:  OUTPUT
#	X:  INPUT and OUTPUT
#
#	V:  Value
#	P:  point ADDR
#
#	S:  single
#	M:  multi
#	
#	D:  data, byte stream flow by number
#

CK_RV
C_LoginUser
(
	IVS CK_SESSION_HANDLE hSession,
	IVS CK_USER_TYPE	  userType,
	IPD CK_UTF8CHAR_PTR	  pPin,
	IVS CK_ULONG	  ulPinLen,
	IPD CK_UTF8CHAR_PTR   pUsername,
	IVS CK_ULONG ulUsernameLen
)
{
	char	str[256] = {0};

	printf("%s......\n", __FUNCTION__);
	printf("userType: %ld\n", userType);
	memcpy(str, pPin, ulPinLen);
	printf("pPin: %s\n", str);
	printf("ulPinLen: %ld\n", ulPinLen);
	memset(str, 0, 256);
	memcpy(str, pUsername, ulUsernameLen);
	printf("pUsername: %s\n", str);
	printf("ulUsernameLen: %ld\n", ulUsernameLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_Logout
(
	IVS CK_SESSION_HANDLE hSession
)
{
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_EncryptInit
(
	IVS CK_SESSION_HANDLE hSession,
	IPS CK_MECHANISM_PTR pMechanism,
	IVS CK_OBJECT_HANDLE hKey
)
{
	char	str[256] = {0};

	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);
	printf("key: %ld\n", hKey);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_EncryptUpdate
(
	IVS CK_SESSION_HANDLE hSession,
	IPD CK_BYTE_PTR	pPart,
	IVS CK_ULONG	ulPartLen,
	OPD CK_BYTE_PTR	pEncryptedPart,
	OPS CK_ULONG_PTR pulEncryptedPartLen
)
{
	printf("%s......\n", __FUNCTION__);
	*pulEncryptedPartLen = ulPartLen;
	memcpy(pEncryptedPart, pPart, ulPartLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_EncryptFinal
(
	IVS CK_SESSION_HANDLE hSession,
	OPD CK_BYTE_PTR	pLastEncryptedPart,
	OPS CK_ULONG_PTR pulLastEncryptedPartLen
)
{
	printf("%s......\n", __FUNCTION__);
	strcpy((char *)pLastEncryptedPart, "ENC final.\n");
	*pulLastEncryptedPartLen = 10;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_Encrypt
(
	IVS CK_SESSION_HANDLE hSession,
	IPD CK_BYTE_PTR	pData,
	IVS CK_ULONG	ulDataLen,
	OPD CK_BYTE_PTR	pEncryptedData,
	OPS CK_ULONG_PTR pulEncryptedDataLen

)
{
	printf("%s......\n", __FUNCTION__);
	*pulEncryptedDataLen = ulDataLen;
	memcpy(pEncryptedData, pData, ulDataLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_DecryptInit
(
	IVS CK_SESSION_HANDLE hSession,
	IPS CK_MECHANISM_PTR pMechanism,
	IVS CK_OBJECT_HANDLE hKey
)
{
	char	str[256] = {0};

	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);
	printf("key: %ld\n", hKey);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_DecryptUpdate
(
	IVS CK_SESSION_HANDLE hSession,
	IPD CK_BYTE_PTR	pEncryptedPart,
	IVS CK_ULONG	ulEncryptedPartLen,
	OPD CK_BYTE_PTR	pPart,
	OPS CK_ULONG_PTR pulPartLen
)
{
	printf("%s......\n", __FUNCTION__);
	*pulPartLen = ulEncryptedPartLen;
	memcpy(pPart, pEncryptedPart, ulEncryptedPartLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_DecryptFinal
(
	IVS CK_SESSION_HANDLE hSession,
	OPD CK_BYTE_PTR	pLastPart,
	OPS CK_ULONG_PTR pulLastPartLen
) 
{
	printf("%s......\n", __FUNCTION__);
	strcpy((char *)pLastPart, "DECF.\n");
	*pulLastPartLen = 4;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_Decrypt
(
	IVS CK_SESSION_HANDLE hSession,
	IPD CK_BYTE_PTR	pEncryptedData,
	IVS CK_ULONG	ulEncryptedDataLen,
	OPD CK_BYTE_PTR	pData,
	OPS CK_ULONG_PTR pulDataLen
)
{
	printf("%s......\n", __FUNCTION__);
	memcpy(pData, pEncryptedData, ulEncryptedDataLen);
	*pulDataLen = ulEncryptedDataLen;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_DigestInit
(
	IVS CK_SESSION_HANDLE hSession,
	IPS CK_MECHANISM_PTR pMechanism
)
{
	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_DigestUpdate
(
	IVS CK_SESSION_HANDLE hSession,
	IPD CK_BYTE_PTR	pPart,
	IVS CK_ULONG ulPartLen
) 
{
	char	str[256] = {0};

	printf("%s......\n", __FUNCTION__);
	printf("C_DigestUpdate len: %ld\n", ulPartLen);
	memcpy(str, pPart, ulPartLen);
	printf("C_DigestUpdate: %s\n", str);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_DigestFinal
(
	IVS CK_SESSION_HANDLE hSession,
	OPD CK_BYTE_PTR	pDigest,
	OPS CK_ULONG_PTR pulDigestLen
) 
{
	printf("%s......\n", __FUNCTION__);
	strcpy((char *)pDigest, "DRESULT");
	*pulDigestLen = 7;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_Digest
(
	IVS CK_SESSION_HANDLE hSession,
	IPD CK_BYTE_PTR	pData,
	IVS CK_ULONG	ulDataLen,
	OPD CK_BYTE_PTR	pDigest,
	OPS CK_ULONG_PTR pulDigestLen
) 
{
	char	str[256] = {0};

	printf("%s......\n", __FUNCTION__);
	printf("inputlen: %ld\n", ulDataLen);
	memcpy(str, pData, ulDataLen);
	printf("input: %s\n", str);
	*pulDigestLen = 16;
	memset(pDigest, 0x31, 16);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_Sign
(
	IVS CK_SESSION_HANDLE hSession,
	IPD CK_BYTE_PTR	pData,
	IVS CK_ULONG	ulDataLen,
	OPD CK_BYTE_PTR	pSignature,
	OPS CK_ULONG_PTR pulSignatureLen
) 
{
	char	str[256] = {0};

	printf("%s......\n", __FUNCTION__);
	memcpy(str, pData, ulDataLen);
	printf("input len: %ld\n", ulDataLen);
	printf("input: %s\n", str);
	memset(pSignature, 0x32, 16);
	*pulSignatureLen = 16;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_Verify
(
	IVS CK_SESSION_HANDLE hSession,
	IPD CK_BYTE_PTR	pData,
	IVS CK_ULONG	ulDataLen,
	IPD CK_BYTE_PTR	pSignature,
	IVS CK_ULONG	ulSignatureLen
)
{
	char	str[256] = {0};

	printf("%s......\n", __FUNCTION__);
	memcpy(str, pData, ulDataLen);
	printf("input data len: %ld\n", ulDataLen);
	printf("input data : %s\n", pData);
	memset(str, 0, 256);
	memcpy(str, pSignature, ulSignatureLen);
	printf("input sign len: %ld\n", ulSignatureLen);
	printf("input sign : %s\n", str);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_GenerateRandom
(
	IVS CK_SESSION_HANDLE hSession,
	OPD CK_BYTE_PTR	pRandomData,
	IVS CK_ULONG	ulRandomLen
) 
{
	printf("%s......\n", __FUNCTION__);
	printf("ulRandomLen: %ld\n", ulRandomLen);
	memset(pRandomData, 0x33, ulRandomLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_GenerateKey
(
	IVS CK_SESSION_HANDLE hSession,
	IPS CK_MECHANISM_PTR pMechanism, 
	IPM CK_ATTRIBUTE_PTR pTemplate,
	IVS CK_ULONG	ulCount,
	OPS CK_OBJECT_HANDLE_PTR phKey
)
{
	int	i;
	char	str[256] = {0};

	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);

	printf("ulCount: %ld\n", ulCount);
	for(i = 0; i < ulCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;

		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str); 
	}

	*phKey = (CK_OBJECT_HANDLE)3344520;

	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_GenerateKeyPair
(
	IVS CK_SESSION_HANDLE hSession,
	IPS CK_MECHANISM_PTR pMechanism,
	IPM CK_ATTRIBUTE_PTR pPublicKeyTemplate,
	IVS CK_ULONG ulPublicKeyAttributeCount,
	IPM CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
	IVS CK_ULONG ulPrivateKeyAttributeCount,
	OPS CK_OBJECT_HANDLE_PTR phPublicKey,
	OPS CK_OBJECT_HANDLE_PTR phPrivateKey
)
{
	int	i;
	char	str[256] = {0};

	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);

	printf("ulPublicKeyAttributeCount: %ld\n", ulPublicKeyAttributeCount);
	for(i = 0; i < ulPublicKeyAttributeCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pPublicKeyTemplate + i;

		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str); 
	}

	printf("ulPrivateKeyAttributeCount: %ld\n", ulPrivateKeyAttributeCount);
	for(i = 0; i < ulPrivateKeyAttributeCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pPrivateKeyTemplate + i;

		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str); 
	}

	*phPublicKey = (CK_OBJECT_HANDLE)7788123;
	*phPrivateKey = (CK_OBJECT_HANDLE)7788321;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_CreateObject
(
	IVS CK_SESSION_HANDLE hSession,
	IPM CK_ATTRIBUTE_PTR pTemplate,
	IVS CK_ULONG	ulCount,
	OPS CK_OBJECT_HANDLE_PTR phObject
)
{
	CK_ULONG i;
	char	str[256];

	printf("%s......\n", __FUNCTION__);
	for(i = 0; i < ulCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;

		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str); 
	}
	*phObject = (CK_OBJECT_HANDLE)888888;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_DestroyObject
(
	IVS CK_SESSION_HANDLE hSession,
	IPS CK_OBJECT_HANDLE hObject
)
{
	printf("%s......\n", __FUNCTION__);
	printf("object: %ld\n", hObject);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_FindObjectsInit
(
	IVS CK_SESSION_HANDLE hSession,
	IPM CK_ATTRIBUTE_PTR pTemplate,
	IVS CK_ULONG ulCount
)
{
	char    str[256] = {0};
	CK_ULONG i;

	printf("%s......\n", __FUNCTION__);
	for(i = 0; i < ulCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;

		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str);
	}
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_FindObjects
(
	IVS CK_SESSION_HANDLE hSession,
	IVS CK_ULONG    ulMaxObjectCount,
	OPM CK_OBJECT_HANDLE_PTR phObject,
	OPS CK_ULONG_PTR pulObjectCount
)
{
	CK_ULONG i;

	printf("%s......\n", __FUNCTION__);
	printf("ulMaxObjectCount: %ld\n", ulMaxObjectCount);
	for(i = 0; i < 8; i++)
	{
		CK_OBJECT_HANDLE_PTR one = phObject + i;
		*one = (CK_OBJECT_HANDLE)(10000 + i);
	}
	*pulObjectCount = 8;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_FindObjectsFinal
(
	IVS CK_SESSION_HANDLE hSession
)
{
	printf("%s ok......\n", __FUNCTION__);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_GetAttributeValue
(
	IVS CK_SESSION_HANDLE hSession,
	IPS CK_OBJECT_HANDLE hObject,
	XPM CK_ATTRIBUTE_PTR pTemplate,
	IVS CK_ULONG	ulCount
)
{
	CK_ULONG i;

	printf("%s......\n", __FUNCTION__);
	printf("ulCount: %ld\n", ulCount);
	for(i = 0; i < ulCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;
		printf("get attr: %ld\n", one->type);
		if(one->ulValueLen > 10)
		{
			sprintf(one->pValue, "getattr%ld", i);
			one->ulValueLen = strlen((char *)one->pValue);
		}

	}
	printf("%s OK.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_SetAttributeValue
(
	IVS CK_SESSION_HANDLE hSession,
	IPS CK_OBJECT_HANDLE hObject,
	IPM CK_ATTRIBUTE_PTR pTemplate,
	IVS CK_ULONG	ulCount
)
{
	char str[256];
	CK_ULONG i;

	printf("%s......\n", __FUNCTION__);
	printf("object: %ld\n", hObject);
	for(i = 0; i < ulCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;

		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str); 
	}
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_WrapKey
(
	IVS CK_SESSION_HANDLE hSession,
	IPS CK_MECHANISM_PTR pMechanism,
	IPS CK_OBJECT_HANDLE hWrappingKey,
	IPS CK_OBJECT_HANDLE hKey,
	OPD CK_BYTE_PTR	pWrappedKey,
	OPS CK_ULONG_PTR pulWrappedKeyLen
)
{
	char str[256] = {0};

	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);

	printf("hWrappingKey = %ld\n", hWrappingKey);
	printf("hKey = %ld\n", hKey);
	*pulWrappedKeyLen = 32;
	memset(pWrappedKey, 0x38, 32);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV
C_UnwrapKey
(
	IVS CK_SESSION_HANDLE hSession,
	IPS CK_MECHANISM_PTR pMechanism,
	IPS CK_OBJECT_HANDLE hUnwrappingKey,
	IPD CK_BYTE_PTR	pWrappedKey,
	IVS CK_ULONG	ulWrappedKeyLen,
	IPM CK_ATTRIBUTE_PTR pTemplate,
	IVS CK_ULONG	ulAttributeCount,
	OPS CK_OBJECT_HANDLE_PTR phKey
)
{
	char str[256] = {0};
	CK_ULONG i;

	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);

	printf("hUnwrappingKey: %ld\n", hUnwrappingKey);
	printf("ulWrappedKeyLen: %ld\n", ulWrappedKeyLen);
	memcpy(str, pWrappedKey, ulWrappedKeyLen);
	printf("pWrappedKey: %s\n", pWrappedKey);

	for(i = 0; i < ulAttributeCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;

		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str);
	}
	*phKey = (CK_OBJECT_HANDLE)99999;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}
